home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Magazine / C_Tutorial / Part-4 / screen4.c < prev    next >
C/C++ Source or Header  |  1997-08-31  |  11KB  |  394 lines

  1. /* First, include our own supporting header files, i.e., clip.h */
  2. #include "clip.h"
  3.  
  4. #include<exec/libraries.h>
  5. #include<intuition/intuition.h>
  6. #include<utility/tagitem.h>
  7. #include<graphics/text.h>
  8. #include<graphics/rastport.h>
  9. #include<intuition/screens.h>
  10. #include<libraries/gadtools.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/exec_protos.h>
  16. #include<clib/graphics_protos.h>
  17. #include<clib/intuition_protos.h>
  18. #include<clib/gadtools_protos.h>
  19.  
  20. /* The library base global variables */
  21. /* (The different style of opening libraries requires these to be initialised to NULL) */
  22. struct Library* GfxBase = NULL;
  23. struct Library* IntuitionBase = NULL;
  24. struct Library* LayersBase = NULL;
  25. struct Library* GadToolsBase = NULL;
  26.  
  27. /* The global handle on the palette gadget */
  28. struct Gadget* palgad = NULL;
  29. /* Global record of foreground pen */
  30. UBYTE pen;
  31.  
  32. /* Need to give prototypes for our functions */
  33. void handleIDCMP(struct Window*);
  34. void setupWindow();
  35. void createWindow(struct Gadget*, struct Screen*, struct Menu*);
  36. int openLibs();
  37. void closeLibs();
  38. struct Menu* createMenuStrip(APTR);
  39. void setFgPen(struct Window*, int);
  40.  
  41. /* Some constants for the position and size of our gadget */
  42. #define MYBUT_LEFT        (10)
  43. #define MYBUT_TOP            (5)
  44. #define MYBUT_WIDTH        (80)
  45. #define MYBUT_HEIGHT    (12)
  46. #define MYBUT_TEXT        "Next Pen"
  47. #define MYBUT_ID            (0)
  48.  
  49. #define MYPAL_LEFT        (170)
  50. #define MYPAL_TOP            (2)
  51. #define MYPAL_WIDTH        (109)
  52. #define MYPAL_HEIGHT    (19)
  53. #define MYPAL_TEXT        "Colour:"
  54. #define MYPAL_ID            (1)
  55. #define MYPAL_DEPTH        (4)
  56.  
  57. /* The top gap required around the gadgets */
  58. #define MYTOPGAP      (30)
  59.  
  60. /* The initial pen colour */
  61. #define MYINITPEN            (1)
  62.  
  63. /* The start of the program */
  64. void main()
  65. {
  66.     /* Use a different style of opening libraries... */
  67.     if(openLibs())
  68.     {
  69.         /* Now do the real work */
  70.         setupWindow();
  71.     }
  72.     /* Matched call to close libraries */
  73.     closeLibs();
  74. }
  75.  
  76. /* Try to open all the libraries -- return TRUE on success */
  77. int openLibs()
  78. {
  79.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  80.     {
  81.         printf("Error: could not open graphics.library\n");
  82.         return FALSE;
  83.     }
  84.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  85.     {
  86.         printf("Error: could not open intuition.library\n");
  87.         return FALSE;
  88.     }
  89.     if((LayersBase = OpenLibrary("layers.library",37)) == NULL)
  90.     {
  91.         printf("Error: could not open layers.library\n");
  92.         return FALSE;
  93.     }
  94.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  95.     {
  96.         printf("Error: could not open gadtools.library\n");
  97.         return FALSE;
  98.     }
  99.   return TRUE;
  100. }
  101.  
  102. /* Close any open library */
  103. void closeLibs()
  104. {
  105.     if(GadToolsBase)
  106.         CloseLibrary(GadToolsBase);
  107.     if(LayersBase)
  108.         CloseLibrary(LayersBase);
  109.     if(IntuitionBase)
  110.         CloseLibrary(IntuitionBase);
  111.     if(GfxBase)
  112.         CloseLibrary(GfxBase);
  113. }
  114.  
  115. /* Setup the window -- do the GadTools stuff */
  116. void setupWindow()
  117. {
  118.     struct Screen* scr;
  119.     UWORD pens[] = { ~0 };
  120.     /* Try to open a new screen with 16 colours (four bitplanes deep) */
  121.     if(scr = OpenScreenTags(NULL,
  122.                                                     SA_Depth,    4,
  123.                                                     /* Enable 3D look by specifying SA_Pens */
  124.                                                     SA_Pens,    pens,
  125.                                                     TAG_DONE))
  126.     {
  127.         APTR vinfo;
  128.         /* Get the visual info so GadTools can render the gadgets nicely */
  129.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  130.         {
  131.             /* We can initialise glist in its declaration */
  132.             struct Gadget* glist = NULL;
  133.             struct Gadget* gad;
  134.             int offtop, offleft;
  135.             struct NewGadget newgad;
  136.             /* Initialised structure declaration: describes 8pt Topaz font */
  137.             struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
  138.             /* Start a GadTools gadget list */
  139.             gad = CreateContext(&glist);
  140.             /* The offsets of our window borders */
  141.             offleft = scr->WBorLeft;
  142.             offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  143.  
  144.             /* Setup our first gadget */
  145.             newgad.ng_TextAttr         = &topazFont;
  146.             newgad.ng_VisualInfo     = vinfo;
  147.             newgad.ng_LeftEdge         = MYBUT_LEFT + offleft;
  148.             newgad.ng_TopEdge         = MYBUT_TOP + offtop;
  149.             newgad.ng_Width             = MYBUT_WIDTH;
  150.             newgad.ng_Height             = MYBUT_HEIGHT;
  151.             newgad.ng_GadgetText    = MYBUT_TEXT;
  152.             newgad.ng_GadgetID        = MYBUT_ID;
  153.             newgad.ng_Flags                = 0;
  154.             /* Now create it and add it to our list */
  155.             gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
  156.  
  157.             /* Setup our second gadget */
  158.             /* (We can reuse newgad, and just change the different bits) */
  159.             newgad.ng_LeftEdge         = MYPAL_LEFT + offleft;
  160.             newgad.ng_TopEdge         = MYPAL_TOP + offtop;
  161.             newgad.ng_Width             = MYPAL_WIDTH;
  162.             newgad.ng_Height             = MYPAL_HEIGHT;
  163.             newgad.ng_GadgetText    = MYPAL_TEXT;
  164.             newgad.ng_GadgetID        = MYPAL_ID;
  165.             newgad.ng_Flags                = 0;
  166.             /* Now create it and add it to our list */
  167.             if(gad = CreateGadget(PALETTE_KIND, gad, &newgad,
  168.                                                         /* Initially selected pen */
  169.                                                         GTPA_Color, MYINITPEN,
  170.                                                         /* Depth: 2 to the power MYPAL_DEPTH colours */
  171.                                                         GTPA_Depth, MYPAL_DEPTH,
  172.                                                         /* Gadget will indicate selection */
  173.                                                         GTPA_IndicatorWidth, 16,
  174.                                                         TAG_DONE))
  175.             {
  176.                 struct Menu* menustrip;
  177.                 /* Remember gadget pointer so we can affect it in message handler */
  178.                 palgad = gad;
  179.                 if(menustrip = createMenuStrip(vinfo))
  180.                     /* If succeeded then all gadgets and menus created */
  181.                     createWindow(glist, scr, menustrip);
  182.                 FreeMenus(menustrip);
  183.             }
  184.             else
  185.                 printf("Error: could not create gadget(s)\n");
  186.             /* Free all the gadgets that were created */
  187.             FreeGadgets(glist);
  188.             FreeVisualInfo(vinfo);
  189.         }
  190.         else
  191.             printf("Error: could not get visual info\n");
  192.         CloseScreen(scr);
  193.     }
  194.     else
  195.         printf("Error: could not create screen\n");
  196. }
  197.  
  198. /* Create the menu strip, using GadTools menu functions */
  199. struct Menu* createMenuStrip(APTR vinfo)
  200. {
  201.     /* The description of our menus */
  202.     struct NewMenu mymenu[] =
  203.     {
  204.         { NM_TITLE, "Project",        0,            0, 0, 0,},
  205.         {  NM_ITEM,        "Quit",                "Q",    0, 0, 0,},
  206.         { NM_TITLE, "Pen",                0,            0, 0, 0,},
  207.         {  NM_ITEM,        "Next",                "N",    0, 0, 0,},
  208.         {  NM_ITEM,        "Prev",                "P",    0, 0, 0,},
  209.         {  NM_ITEM,        NM_BARLABEL,    0,        0, 0, 0,},
  210.         {  NM_ITEM,        "Reset",            "R",    0, 0, 0,},
  211.         {   NM_END, NULL,                    0,            0, 0, 0,},
  212.     };
  213.     struct Menu* menustrip;
  214.     if (menustrip = CreateMenus(mymenu, TAG_END))
  215.     {
  216.         if (LayoutMenus(menustrip, vinfo, TAG_END))
  217.             /* Succeeded, so return menu strip */
  218.             return menustrip;
  219.         else
  220.         {
  221.             /* Failed, so must deallocate before returning */
  222.             FreeMenus(menustrip);
  223.             printf("Error: could not layout menus\n");
  224.         }
  225.     }
  226.     else
  227.         printf("Error: could not create menu strip\n");
  228.     /* Failed, so return NULL */
  229.     return NULL;
  230. }
  231.  
  232. /* Actually open the window, in the normal way */
  233. void createWindow(struct Gadget* glist, struct Screen* scr, struct Menu* menustrip)
  234. {
  235.     struct Window* win;
  236.     /* Open our window */
  237.     if(win = OpenWindowTags(NULL,
  238.                                                     WA_Left,                    0,
  239.                                                     WA_Top,                        0,
  240.                                                     /* Make the window the same size as the screen */
  241.                                                     WA_Width,                    scr->Width,
  242.                                                     WA_Height,                scr->Height,
  243.                                                     WA_Flags,                    WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE,
  244.                                                     WA_IDCMP,                    IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | BUTTONIDCMP | IDCMP_REFRESHWINDOW | IDCMP_MENUPICK,
  245.                                                     WA_Gadgets,                glist,
  246.                                                     WA_CustomScreen,    scr,
  247.                                                     TAG_DONE,                    0))
  248.     {
  249.         /* If window opened, set clip region */
  250.         if(setClipInternal(win))
  251.         {
  252.             /* Attach menu strip to window */
  253.             if(SetMenuStrip(win, menustrip))
  254.             {
  255.                 /* Let GadTools refresh its bits of the window */
  256.                 GT_RefreshWindow(win, NULL);
  257.                 /* Now handle messages */
  258.                 handleIDCMP(win);
  259.                 /* Remove menu strip */
  260.                 ClearMenuStrip(win);
  261.             }
  262.             else
  263.                 printf("Error: could not attach menus to window\n");
  264.             removeClip(win);    
  265.         }
  266.         else
  267.             printf("Error: could not set clip region on window\n");
  268.         CloseWindow(win);
  269.     }
  270.     else
  271.         printf("Error: could not open window\n");
  272. }
  273.  
  274. /* Our message handling code */
  275. void handleIDCMP(struct Window* win)
  276. {
  277.     char* text = "Hello World!";
  278.     int going = TRUE;
  279.     int drawing = FALSE;
  280.     setFgPen(win, MYINITPEN);
  281.     /* Set the drawing mode to draw only the foreground of text, not the background */
  282.     SetDrMd(win->RPort, JAM1);
  283.     while(going)
  284.     {
  285.         struct IntuiMessage* intuimsg;
  286.         /* Wait for messages to arrive */
  287.         WaitPort(win->UserPort);
  288.         /* Messages have arrived: loop through all of them */
  289.         while(intuimsg = GT_GetIMsg(win->UserPort))
  290.         {
  291.             /* Act on this message... */
  292.             switch(intuimsg->Class)
  293.             {
  294.             case IDCMP_MOUSEBUTTONS:
  295.                 switch(intuimsg->Code)
  296.                 {
  297.                 case SELECTDOWN:
  298.                     drawing = TRUE;
  299.                     break;
  300.                 case SELECTUP:
  301.                     drawing = FALSE;
  302.                     break;
  303.                 }
  304.                 /* break; omitted so we draw on click, too */
  305.             case IDCMP_MOUSEMOVE:
  306.                 /* Don't draw on top gap which holds gadgets */
  307.                 if(drawing && intuimsg->MouseY > win->BorderTop+MYTOPGAP)
  308.                 {
  309.                     Move(win->RPort, intuimsg->MouseX, intuimsg->MouseY);
  310.                     Text(win->RPort, text, strlen(text));
  311.                 }
  312.                 break;
  313.             case IDCMP_CLOSEWINDOW:
  314.                 going = FALSE;
  315.                 break;
  316.             case IDCMP_REFRESHWINDOW:
  317.                 /* You *MUST* remember to ask for and handle these refresh messages */
  318.                 GT_BeginRefresh(win);
  319.                 GT_EndRefresh(win, TRUE);
  320.                 break;
  321.             case IDCMP_GADGETUP:
  322.                 /* Trick: introduce new "{..}" scope so we can declare gad locally */
  323.                 {
  324.                     struct Gadget* gad = (struct Gadget*)(intuimsg->IAddress);
  325.                     switch(gad->GadgetID)
  326.                     {
  327.                     case MYBUT_ID:
  328.                         /* Our button was clicked!  Set foreground to next pen colour */
  329.                         setFgPen(win, pen+1);
  330.                         break;
  331.                     case MYPAL_ID:
  332.                         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  333.                         setFgPen(win, intuimsg->Code);
  334.                         break;
  335.                     }
  336.                     break;
  337.                 }
  338.             case IDCMP_MENUPICK:
  339.                 /* Again, use trick to introduce new "{..}" scope */
  340.                 {
  341.                     UWORD menuCode, menuNumber, itemNumber;
  342.                     /* Loop over all the menu selections in the menu code */
  343.                     for(menuCode = intuimsg->Code;
  344.                             going && menuCode != MENUNULL;
  345.                             menuCode = ItemAddress(win->MenuStrip, menuCode)->NextSelect)
  346.                     {
  347.                         /* Extract the menu number and menu item number from the menu code */
  348.                         menuNumber = MENUNUM(menuCode);
  349.                         itemNumber = ITEMNUM(menuCode);
  350.                         /* Now decide what to do based on what menu item was selected */
  351.                         switch(menuNumber)
  352.                         {
  353.                         case 0:  /* Project menu */
  354.                             /* Only one item: Quit */
  355.                             if(itemNumber == 0)
  356.                                 going = FALSE;
  357.                             break;
  358.                         case 1:  /* Pen menu */
  359.                             switch(itemNumber)
  360.                             {
  361.                             case 0:  /* Next */
  362.                                 setFgPen(win, pen+1);
  363.                                 break;
  364.                             case 1:  /* Prev */
  365.                                 setFgPen(win, pen-1);
  366.                                 break;
  367.                             case 3:  /* Reset (item 2 is the bar!)*/
  368.                                 setFgPen(win, MYINITPEN);
  369.                                 break;
  370.                             }
  371.                             break;
  372.                         }
  373.                     }
  374.                     break;
  375.                 }
  376.             }
  377.             /* Reply when finished with message */
  378.             GT_ReplyIMsg(intuimsg);
  379.         }
  380.     }
  381. }
  382.  
  383. void setFgPen(struct Window* win, int value)
  384. {
  385.     /* Wrap when reached the end of the palette gadget's colours */
  386.     pen = value % (1<<MYPAL_DEPTH);
  387.     SetAPen(win->RPort, pen);
  388.     if(palgad)
  389.     {
  390.         /* Update palette gadget with new pen value */
  391.         GT_SetGadgetAttrs(palgad, win, NULL, GTPA_Color, pen, TAG_DONE);
  392.     }
  393. }
  394.